package org.example.transaction.controller;


import org.example.transaction.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/user1")
public class LoginController2 {


    @Autowired
    private UserInfoService userInfoService;


    /*
    * 没有异常，则添加成功，如果出现异常，则进入回滚
    * */
    @Transactional
    @RequestMapping("/Login")
    public String login(String userName,String password){
        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        return "增加成功";
    }


    /*
     * 如果出现异常，则进入回滚
     *  */
    @Transactional
    @RequestMapping("/Login1")
    public String login1(String userName,String password){

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        Integer a = 10/0;
        return "增加成功";
    }


    /*
     * 如果使用try-catch解决了，那么就不会触发回滚
     *  */
    @Transactional
    @RequestMapping("/Login2")
    public String login2(String userName,String password){

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        try{
            Integer a = 10/0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "增加成功";
    }


    /*
     * 如果使用try-catch，但是将异常再次抛出，那么也会触发回滚
     *  */
    @Transactional
    @RequestMapping("/Login3")
    public String login3(String userName,String password){

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        try{
            Integer a = 10/0;
        } catch (Exception e) {
            //将异常再次抛出
            throw e;
        }
        return "增加成功";
    }

    /*
     * 如果使用try-catch，处理的时候进行了手动回滚，也会添加失败
     *  */
    @Transactional
    @RequestMapping("/Login4")
    public String login4(String userName,String password){

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        try{
            Integer a = 10/0;
        } catch (Exception e) {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "增加成功";
    }


    /*
     * 不会回滚
     * 并不是所有的异常都会回滚，只有运行时异常和
     *  */
    @Transactional
    @RequestMapping("/Login5")
    public String login5(String userName,String password) throws IOException {

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        if(true){
            throw new IOException();
        }
        return "增加成功";
    }

    /*
     * 回滚
     * 并不是所有的异常都会回滚，只有运行时异常和error
     *  */
    @Transactional
    @RequestMapping("/Login6")
    public String login6(String userName,String password) throws IOException {

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        if(true){
            throw new RuntimeException();
        }
        return "增加成功";
    }

    /*
     * 原本不会回滚，经过处理恢回滚
     * rollbackFor指明处理异常的范围
     * isolation设置隔离级别
     *  */
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT )
    @RequestMapping("/Login7")
    public String login7(String userName,String password) throws IOException {

        Integer n = userInfoService.insertUserInfo(userName,password);
        System.out.println("影响的行数："+n);
        if(true){
            throw new IOException();
        }
        return "增加成功";
    }
}
